<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="新闻状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择新闻状态">
          <el-option
            v-for="item in statusList"
            :key="item.val"
            :label="item.label"
            :value="item.val"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="新闻类型" prop="typeName">
        <el-select clearable v-model="queryParams.typeName" placeholder="请选择新闻类型">
          <el-option
            v-for="item in selecTypeList"
            :key="item.typeName"
            :label="item.typeName"
            :value="item.typeName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻标题" prop="newsTitle">
        <el-input
          v-model="queryParams.newsTitle"
          placeholder="请输入新闻标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['portal:news:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['portal:news:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['portal:news:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['portal:news:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="newsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="新闻标题" align="center" show-overflow-tooltip prop="newsTitle" />
      <el-table-column label="新闻图片" align="center" prop="newsPic">
        <template slot-scope="scope">
          <img
            :src="scope.row.newsPic"
            alt
            style="width: 100px; height: 100px; object-fit: cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="新闻状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{scope.row.status==='1'?'关闭':'正常'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="新闻类型" align="center" prop="typeName" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column
        label="新闻简介"
        align="center"
        prop="newsIntroduction"
        show-overflow-tooltip
      />
      <!-- 发布时间 -->
      <el-table-column label="发布日期" align="center" prop="publicTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['portal:news:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['portal:news:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listNews, delNews } from "@/api/portal/news";
import { listType } from "@/api/portal/type";
export default {
  name: "News",
  data() {
    return {
      // 遮罩层
      loading: true,
      userRole:'all',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻表格数据
      newsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        newsPic: null,
        typeName: null,
        author: null,
        newsTitle: null,
        newsIntroduction: null,
        originalAddress: null,
        newsContent: null,
      },

      selecTypeList: [],
      statusList: [
        {label:'正常',val:'0'},
        {label:'关闭',val:'1'},
      ],
    };
  },
  created() {
    this.userRole=this.$store.getters.roles[0]
    this.getList();
    this.getnewstypeList();
  },
  methods: {
    /** 查询新闻列表 */
    getList() {
      this.loading = true;
      if (this.userRole=="zhencexinwen"){
        this.queryParams.typeName="政策速递"
      }
      listNews(this.queryParams).then((response) => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: "Editnews" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: "Editnews",
        query: { id: row.id || this.ids },
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除新闻编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delNews(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "portal/news/export",
        {
          ...this.queryParams,
        },
        `portal_news.xlsx`
      );
    },

    /** 查询新闻类型列表 */
    getnewstypeList() {
      listType(this.queryParams).then((response) => {
        this.selecTypeList = response.rows;
      });
    },
  },
};
</script>
